package com.wang.mandkey.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.wang.mandkey.R;
import com.wang.mandkey.net.Connecter;
import com.wang.mandkey.net.ConnecterPool;
import com.wang.mandkey.net.INetCallBack;
import com.wang.mandkey.tool.RemoteOperateImpl;
import com.wang.mandkey.tool.Tool;
import com.wang.mandkey.utils.ProjectEnvironment;
import com.wang.mandkey.utils.StringUtils;
import com.waps.AppConnect;
import com.waps.MiniAdView;

public class RemoteToolActivity extends BaseActivity implements INetCallBack {

	private RemoteOperateImpl mRemoteOperateImpl;
	private Connecter mConnector;
	private EditText mEditTextDosInput;
	private Button mButtonExec;
	private Button mButtonShutdown;
	private Button mButtonCancellShutdown;
	private Button mButtonLockScrren;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.remote_tool);
		setUpView();
		mConnector = ConnecterPool.getConnectorByKey(ConnecterPool.STRING_CKEY);
		if (null != mConnector) {
			mRemoteOperateImpl = new RemoteOperateImpl(mConnector, this);
		} else {
			Toast.makeText(this,
					getResources().getString(R.string.app_lost_host),
					Toast.LENGTH_SHORT).show();
		}
		ProjectEnvironment.BOOLEAN_LOCK_KEYBOAED = true;
	}

	@Override
	void setUpView() {

		mEditTextDosInput = (EditText) findViewById(R.id.editTextShell);
		mButtonExec = (Button) findViewById(R.id.buttonSendShell);
		mButtonExec.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				String dos = mEditTextDosInput.getText().toString();
				if (!StringUtils.isEmpty(dos)) {
					mRemoteOperateImpl.sendCommandDos(dos);
				} else {
					Animation shak = AnimationUtils.loadAnimation(
							RemoteToolActivity.this, R.anim.shake);
					mEditTextDosInput.startAnimation(shak);
				}
			}
		});

		mButtonShutdown = (Button) findViewById(R.id.buttonShutdown);
		mButtonShutdown.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mRemoteOperateImpl.sendCommandDos("shutdown -s -t 30");
			}
		});
		mButtonCancellShutdown = (Button) findViewById(R.id.buttonShutdownCancell);
		mButtonCancellShutdown.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mRemoteOperateImpl.sendCommandDos("shutdown -a");
			}
		});

		mButtonLockScrren = (Button) findViewById(R.id.buttonLockScreen);
		mButtonLockScrren.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mRemoteOperateImpl
						.sendCommand(ProjectEnvironment.STRING_COMMAND_LOCK_SCREEN);
			}
		});
		// try {
		// //该死的广告,害怕它出什么问题
		// int[] adtype={1,2,3};
		// ad=new ImpressionAdView(this,linearLayoutRoot, 0, 0, 0xffffffff,
		// false, adtype);
		// ad.show(60);
		// } catch (Exception e) {
		// e.printStackTrace();
		// Log.e(tag, "e:"+e.getMessage());
		// }

		// buttonLockScreen=(Button)findViewById(R.id.buttonShutdownLock);
		// buttonLockScreen.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// // TODO Auto-generated method stub
		// remoteOperateImpl.sendCommand(ProjectEnvironment.STRING_COMMAND_LOCK_SCREEN);
		// Log.i(tag, "lock screen");
		// }
		// });
		AppConnect.getInstance(this).setAdBackColor(Color.argb(50, 120, 240, 120));
		// 设置迷你广告广告语颜色
		AppConnect.getInstance(this).setAdForeColor(Color.YELLOW);
		// 若未设置以上两个颜色，则默认为黑底白字
		LinearLayout miniLayout = (LinearLayout) findViewById(R.id.miniAdLinearLayout);
		new MiniAdView(this, miniLayout).DisplayAd(10); // 默认10秒切换一次广告
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		// try {
		// ad.close();
		// } catch (Exception e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
	}

	@Override
	public void OnFinish() {

	}

	@Override
	public void OnIntercepted(String source) {
		Tool.doNetLost(this);
	}

	@Override
	public void OnStart() {
		// TODO Auto-generated method stub

	}
}
